﻿
Imports elpmis

'not fully necessary, just show the usage of scriptparser & scriptmonitor<T>.invokeDelegate
Public Class lpmsmonitor
    Inherits scriptmonitor(Of lpmsapp)

    Private Shared parser As scriptparser = Nothing

    Shared Sub New()
        parser = New scriptparser(AddressOf lpmsapp.parse)
    End Sub

    Public Sub New(ByVal scriptfile As String)
        MyBase.New(scriptfile, parser)
    End Sub

    Private Class initialInvoke
        Inherits invokeDelegate

        Private config As configParser = Nothing

        Protected Overrides Function invokeImpl(ByVal app As lpmsapp, ByRef rtn As bare_variable) As Boolean
            Return app.initial(config, rtn)
        End Function

        Public Sub New(ByVal app As lpmsmonitor, ByVal config As configParser)
            MyBase.New(app)
            Me.config = config
        End Sub

        Public Overrides Function functionName() As String
            Return "initial"
        End Function
    End Class

    Private Class headerInvoke
        Inherits invokeDelegate

        Protected Overrides Function invokeImpl(ByVal app As lpmsapp, ByRef rtn As bare_variable) As Boolean
            Return app.header(rtn)
        End Function

        Public Sub New(ByVal app As lpmsmonitor)
            MyBase.New(app)
        End Sub

        Public Overrides Function functionName() As String
            Return "header"
        End Function
    End Class

    Private Class processInvoke
        Inherits invokeDelegate

        Protected Overrides Function invokeImpl(ByVal app As lpmsapp, ByRef rtn As bare_variable) As Boolean
            Return app.process(rtn)
        End Function

        Public Sub New(ByVal app As lpmsmonitor)
            MyBase.New(app)
        End Sub

        Public Overrides Function functionName() As String
            Return "process"
        End Function
    End Class

    Private Class pathInvoke
        Inherits invokeDelegate

        Protected Overrides Function invokeImpl(ByVal app As lpmsapp, ByRef rtn As bare_variable) As Boolean
            Return app.path(rtn)
        End Function

        Public Overrides Function functionName() As String
            Return "path"
        End Function

        Public Sub New(ByVal app As lpmsmonitor)
            MyBase.New(app)
        End Sub
    End Class

    Private Class cacheInvoke
        Inherits invokeDelegate

        Protected Overrides Function invokeImpl(ByVal app As lpmsapp, ByRef rtn As bare_variable) As Boolean
            Return app.cache(rtn)
        End Function

        Public Overrides Function functionName() As String
            Return "cache"
        End Function

        Public Sub New(ByVal app As lpmsmonitor)
            MyBase.New(app)
        End Sub
    End Class

    Private Class renderInvoke
        Inherits invokeDelegate

        Private _parameters As iParameters = Nothing

        Protected Overrides Function parameters() As iParameters
            Return _parameters
        End Function

        Protected Overrides Function invokeImpl(ByVal app As lpmsapp, ByRef rtn As bare_variable) As Boolean
            Return app.render(parameters, rtn)
        End Function

        Public Overrides Function functionName() As String
            Return "render"
        End Function

        Public Sub New(ByVal app As lpmsmonitor, Optional ByVal parameters As iParameters = Nothing)
            MyBase.New(app)
            _parameters = parameters
        End Sub
    End Class

    Private Class globalInitialInvoke
        Inherits invokeDelegate

        Protected Overrides Function invokeImpl(ByVal app As lpmsapp, ByRef rtn As bare_variable) As Boolean
            Return app.global_initial(rtn)
        End Function

        Public Overrides Function functionName() As String
            Return globalInitial
        End Function

        Public Sub New()
            MyBase.New(shareapp)
        End Sub
    End Class

    Public Shared Function invokeinitial(ByVal app As lpmsmonitor, ByVal config As configParser) As Boolean
        Return invoke(New initialInvoke(app, config))
    End Function

#If RETIRED Then
    Public Shared Function invokeheader(ByVal app As lpmsmonitor, ByVal context As httpContext) As Boolean
        If invoke(New headerInvoke(app)) Then
            context.addResponseHeader(Net.HttpResponseHeader.Server, applicationSign())
            context.modifyResponseHeader(network.http.headers.PoweredBy, enginename())
            Return True
        Else
            Return False
        End If
    End Function
#End If

    Public Shared Function invokeprocess(ByVal app As lpmsmonitor) As Boolean
        Dim rtn As bare_variable = Nothing
        If invoke(New processInvoke(app), rtn) Then
            If rtn Is Nothing OrElse rtn.false() Then
                raiseError("script returns false after invokeprocess.", errorHandle.errorType.user)
                Return False
            Else
                Return True
            End If
        Else
            Return False
        End If
    End Function

    Public Shared Function invokepath(ByVal app As lpmsmonitor, ByRef path As String) As Boolean
        Dim rst As bare_variable = Nothing
        If invoke(New pathInvoke(app), rst) AndAlso Not rst Is Nothing Then
            path = Convert.ToString(rst.value())
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function invokecache(ByVal app As lpmsmonitor, ByRef ticks As Int64) As Boolean
        Dim rst As bare_variable = Nothing
        Return invoke(New cacheInvoke(app), rst) AndAlso _
               Not rst Is Nothing AndAlso _
               convertor.toint64(rst.value(), ticks)
    End Function

    Public Shared Function invokerender(ByVal app As lpmsmonitor, ByRef rtn As String, _
                                        Optional ByVal parameters As iParameters = Nothing) As Boolean
        Dim rst As bare_variable = Nothing
        If invoke(New renderInvoke(app, parameters), rst) AndAlso Not rst Is Nothing Then
            rtn = Convert.ToString(rst.value())
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function invokeglobalinitial() As Boolean
        Return Not shareapp Is Nothing AndAlso invoke(New globalInitialInvoke())
    End Function

    Private Sub lpmsmonitor_scriptChanged() Handles Me.scriptChanged
        If Me Is shareapp Then
            'if shareapp changed before any invoke, some lpmsengiFunctions may not be registered,
            'should just ignore them.
            invokeglobalinitial()
        End If
    End Sub
End Class
